// pages/phone-login/phone-login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: '获取验证码',
    time: 59,
    isSelected: false,
    navigationSetting: {
      title: '',
      height: app.globalData.navBarHeight,
      paddingTop: app.globalData.statusBarHeight,
      backgroundColor: 'white',
      size: 'default',
      back: '<'
    },
    formData: {
      phoneNumber: '',
      phoneCode: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  bindPhoneInput: function (e) {
    this.setData({
      'formData.phoneNumber': e.detail.value
    })
  },
  bindCodeInput: function (e) {
    this.setData({
      'formData.phoneCode': e.detail.value
    })
  },
  countdown() {
    let time = this.data.time
    time < 10 ? time = '0' + time : '';
    this.setData({
      countdown: time,
      time: time - 1
    })
    if (time > 1) {
      setTimeout(this.countdown, 1000);
    } else {
      this.setData({
        countdown: '获取验证码'
      })
    }
  },
  gotoPrivateFile() {
    wx.navigateTo({
      url: '/pages/html/html?type=' + 1,
    })
  },
  gotoUserFile() {
    wx.navigateTo({
      url: '/pages/html/html?type=' + 2,
    })
  },
  getCode() {
    if (this.data.countdown !== '获取验证码') {
      return false

    }
    let phoneNumber = this.data.formData.phoneNumber
    console.log(32323)
    let checked = this.checked()
    if ('phone false' === checked) {
      wx.showToast({
        title: '手机号码不能为空！',
        icon: 'none',
      })
      return false
    } else if ('phone length false' === checked) {
      wx.showToast({
        title: '手机号码必须为11位',
        icon: 'none',
      })
      return false
    } else if ('sel false' === checked) {
      wx.showToast({
        title: '须先阅读并同意相关协议',
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return false
    }
    this.data.time = 59
    this.countdown()
    app.login.getCode({
      phoneNumber: phoneNumber
    }).then((res) => {
      if (res.code === 200) {
        wx.showToast({
          title: '验证码已发送',
        })
        app.globalData.loginPhoneNumber = phoneNumber
      }
    })

  },
  checked() {
    let phoneNumber = this.data.formData.phoneNumber
    if (phoneNumber === '') {
      return 'phone false'
    }
    if (phoneNumber.length !== 11) {
      return 'phone length false'
    }
    if (!this.data.isSelected) {

      return 'sel false'
    }
    return true
  },
  login() {
    let phoneNumber = this.data.formData.phoneNumber
    let checked = this.checked()
    if ('phone false' === checked) {
      wx.showToast({
        title: '手机号码不能为空！',
        icon: 'none',
      })
      return false
    } else if ('phone length false' === checked) {
      wx.showToast({
        title: '手机号码必须为11位',
        icon: 'none',
      })
      return false
    } else if ('sel false' === checked) {
      wx.showToast({
        title: '须先阅读并同意相关协议',
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return false
    }

    let phoneCode = this.data.formData.phoneCode
    if (phoneCode === '') {
      wx.showToast({
        title: '验证码不能为空！',
        icon: 'none',
      })
      return false
    }

    if (phoneCode.length !== 6) {
      wx.showToast({
        title: '验证码必须为6位！',
        icon: 'none',
      })
      return false
    }
    wx.login({
      success(res) {
        if (res.code) {
          console.log('获取到的code:', res.code);
          app.login.loginByCode({
            phoneNumber: phoneNumber,
            code: phoneCode,
            loginCode: res.code
          }).then(res => {
            if (res.code === 200) {
              wx.setStorageSync('token', res.data)
              wx.switchTab({
                url: '/pages/homepage/homepage',
              })
            } else {
              wx.showToast({
                title: '验证码错误！',
                icon: 'none',
              })
            }
          })
        }
      }
    });


  },
  selBox(e) {
    this.setData({
      isSelected: !this.data.isSelected
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let phoneNumber = app.globalData.loginPhoneNumber
    if (phoneNumber != undefined && phoneNumber != null && phoneNumber != '') {
      this.setData({
        'formData.phoneNumber': phoneNumber
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})